//
//  ParserTOTEM.java
//  TopoManipApplet
//
//  Created by Thibaud PIQUET on 23/03/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
package src.convertisseur;

import java.util.*;
import java.io.*;
import java.lang.*;
import org.jdom.*;
import javax.swing.*;
import java.math.BigInteger;

/**
 *
 * @author Thibaud PIQUET
 * Version 1.1 - Ksoobrayen : Evolutions vers un fichier graphML --> Le 06 mars 2011
 */
public class ParserTOTEM implements Runnable {

    File ftp; // File to parse
    File fileOut; // File to create

    String[] keyGraphNode = {"name","switch","rid", "nodeType", "location", "interfaces"};
    String[] keyGraphEdge = {"src", "tgt", "description","status","bandwidth","delay","linkType","srlgs","technology"};
    

    public ParserTOTEM(File file, File fileOUT) throws IOException {
        this.ftp = file;
        this.fileOut = fileOUT;
    }

    public void run() {
        try {
            this.parseToXML();
        } catch (Exception e) {
            e.printStackTrace();
            Resume result = Resume.getInstance();
            result.add("ParserTOTEM", "procedure run had some problems : " + e);
        }
    }

    public void parseToXML() throws IOException {

        Element domainTOTEM;
        Element informationTOTEM;
        Element topologyTOTEM;
        Element igpTOTEM;
        Element bgpTOTEM;
        Element mplsTOTEM;
        Element nodesTOTEM;
        Element linksTopoTOTEM;
        Element igpLinksTOTEM;
        Element bgpRoutersTOTEM;

        Element graphml = new Element("graphml");
        Namespace nsGraphml = Namespace.getNamespace("http://graphml.graphdrawing.org/xmlns");
        graphml.setNamespace(nsGraphml);


        Element graph = new Element("graph");
        Attribute edgedefault = new Attribute("edgedefault", "undirected");
        graph.setAttribute(edgedefault);

        Element domain = new Element("domain");
        Element information = new Element("information");
        Element topology = new Element("topology");
        Element igp = new Element("igp");
        Element bgp = new Element("bgp");
        Element mpls = new Element("mpls");
        Element nodes = new Element("nodes");
        Element links = new Element("links");
        Element igpLinks = new Element("links");
        Element bgpRouters = new Element("routers");

        for (int i = 0; i < keyGraphNode.length; i++) {
                topology.addContent(Utils.getKeyGraph(i, keyGraphNode, "node"));

        }

        for (int i = 0; i < keyGraphEdge.length; i++) {
            topology.addContent(Utils.getKeyGraph(i, keyGraphEdge, "edge"));
        }


        double unitBandwidth = 1;  // in megabits
        double unitDelay = 1;      // in ms

        org.jdom.Document document = new Document(graphml);

        Resume result = Resume.getInstance();

        try {

            org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
            builder.setIgnoringElementContentWhitespace(true);
            org.jdom.Document documentTOTEM = builder.build(ftp);

            domainTOTEM = documentTOTEM.getRootElement();

            /* Test to know domain son's */
            boolean BoolInformation = false;
            boolean BoolIGP = false;
            boolean BoolBGP = false;
            boolean BoolMPLS = false;
            boolean BoolTopology = false;

            List domainChildren = domainTOTEM.getChildren();
            Iterator idomainChildren = domainChildren.iterator();
            while (idomainChildren.hasNext()) {
                Element currentChild = (Element) idomainChildren.next();
                if (currentChild.toString().equals("[Element: <info/>]")) {
                    BoolInformation = true;
                }
                if (currentChild.toString().equals("[Element: <igp/>]")) {
                    BoolIGP = true;
                }
                if (currentChild.toString().equals("[Element: <bgp/>]")) {
                    BoolBGP = true;
                }
                if (currentChild.toString().equals("[Element: <mpls/>]")) {
                    BoolMPLS = true;
                }
                if (currentChild.toString().equals("[Element: <topology/>]")) {
                    BoolTopology = true;
                }
            }

            if (BoolInformation == true) {
                informationTOTEM = domainTOTEM.getChild("info");

                List infoChildren = informationTOTEM.getChildren();
                Iterator iInfoChildren = infoChildren.iterator();
                while (iInfoChildren.hasNext()) {
                    Element currentChild = (Element) iInfoChildren.next();
                    /* case title */
                    if (currentChild.toString().equals("[Element: <title/>]")) {
                        Element title = new Element("title");
                        title.setText(currentChild.getText());
                        information.addContent(title);
                    }
                    /* case date */
                    if (currentChild.toString().equals("[Element: <date/>]")) {
                        Element date = new Element("date");
                        date.setText(currentChild.getText());
                        information.addContent(date);
                    }
                    /* case author */
                    if (currentChild.toString().equals("[Element: <author/>]")) {
                        Element author = new Element("author");
                        author.setText(currentChild.getText());
                        information.addContent(author);
                    }
                    /* case description */
                    if (currentChild.toString().equals("[Element: <description/>]")) {
                        Element description = new Element("description");
                        description.setText(currentChild.getText());
                        information.addContent(description);
                    }
                    /* case units */
                    if (currentChild.toString().equals("[Element: <units/>]")) {

                        List listOfunit = currentChild.getChildren();
                        Iterator ilistOfunit = listOfunit.iterator();
                        while (ilistOfunit.hasNext()) {
                            Element currentUnit = (Element) ilistOfunit.next();

                            if (currentUnit.getAttributeValue("type").equals("bandwidth")) {
                                if (currentUnit.getAttributeValue("value").equals("bps")) {   // bits per second
                                    unitBandwidth = 0.000001;
                                }
                                if (currentUnit.getAttributeValue("value").equals("kbps")) {  // kilobits per second
                                    unitBandwidth = 0.001;
                                }
                                if (currentUnit.getAttributeValue("value").equals("mbps")) {  // megabits per second
                                    unitBandwidth = 1;
                                }
                                if (currentUnit.getAttributeValue("value").equals("gbps")) {  // gigabits per second
                                    unitBandwidth = 1000;

                                }
                                if (currentUnit.getAttributeValue("value").equals("tbps")) {  // terabits per second
                                    unitBandwidth = 1000000;
                                }
                            }
                            if (currentUnit.getAttributeValue("type").equals("delay")) {
                                if (currentUnit.getAttributeValue("value").equals("ns")) {    // nanoseconds
                                    unitDelay = 0.000001;
                                }
                                if (currentUnit.getAttributeValue("value").equals("ï¿½s")) {  // microseconds
                                    unitDelay = 0.001;
                                }
                                if (currentUnit.getAttributeValue("value").equals("ms")) {    // milliseconds
                                    unitDelay = 1;
                                }
                                if (currentUnit.getAttributeValue("value").equals("s")) {     // seconds
                                    unitDelay = 1000;
                                }
                            }
                        }
                    }
                    /* case diff-serv */
                    if (currentChild.toString().equals("[Element: <diff-serv/>]")) {
                        Element diffServ = new Element("diff-serv");

                        List listOfPriority = currentChild.getChildren();
                        Iterator ilistOfPriority = listOfPriority.iterator();
                        while (ilistOfPriority.hasNext()) {
                            Element currentPriority = (Element) ilistOfPriority.next();
                            Element priority = new Element("priority");
                            Attribute ct = new Attribute("ct", currentPriority.getAttributeValue("ct"));
                            Attribute id = new Attribute("id", currentPriority.getAttributeValue("id"));
                            Attribute preemption = new Attribute("preemption", currentPriority.getAttributeValue("preemption"));
                            priority.setAttribute(ct);
                            priority.setAttribute(id);
                            priority.setAttribute(preemption);
                            diffServ.addContent(priority);
                        }
                        information.addContent(diffServ);
                    }
                    /* case classes-of-service */
                    if (currentChild.toString().equals("[Element: <classes-of-service/>]")) {
                        Element classesServ = new Element("classes-of-service");

                        List listOfCos = currentChild.getChildren();
                        Iterator ilistOfCos = listOfCos.iterator();
                        while (ilistOfCos.hasNext()) {
                            Element currentCos = (Element) ilistOfCos.next();
                            Element cos = new Element("cos");

                            List listOfSubClass = currentCos.getChildren();
                            Iterator ilistOfSubClass = listOfSubClass.iterator();
                            while (ilistOfSubClass.hasNext()) {
                                Element currentSubClass = (Element) ilistOfSubClass.next();
                                Element subClass = new Element("sub-class");
                                subClass.setText(currentSubClass.getText());
                                cos.addContent(subClass);
                            }

                            Attribute name = new Attribute("name", currentCos.getAttributeValue("name"));
                            cos.setAttribute(name);
                            classesServ.addContent(cos);
                        }
                        information.addContent(classesServ);
                    }
                    /* case srlgs */
                    if (currentChild.toString().equals("[Element: <srlgs/>]")) {
                        Element srlgs = new Element("srlgs");

                        List listOfSrlg = currentChild.getChildren();
                        Iterator ilistOfSrlg = listOfSrlg.iterator();
                        while (ilistOfSrlg.hasNext()) {
                            Element currentSrlg = (Element) ilistOfSrlg.next();
                            Element srlg = new Element("srlg");
                            Element id = new Element("id");
                            id.setText(currentSrlg.getChildText("id"));
                            srlg.addContent(id);
                            srlgs.addContent(srlg);
                        }
                        information.addContent(srlgs);
                    }
                }
            }

            /* Topology */
            if (BoolTopology == true) {
                topologyTOTEM = domainTOTEM.getChild("topology");
                nodesTOTEM = topologyTOTEM.getChild("nodes");
                linksTopoTOTEM = topologyTOTEM.getChild("links");

                /* nodes */
                List listOfnode = nodesTOTEM.getChildren();
                Iterator ilistOfnode = listOfnode.iterator();
                while (ilistOfnode.hasNext()) {
                    Element currentNode = (Element) ilistOfnode.next();
                    Element node = new Element("node");
                    Element description = new Element("description");
                    Element status = new Element("status");
                    Element rid = new Element("rid");
                    Element nodeType = new Element("nodeType");
                    Element location = new Element("location");
                    Element interfaces = new Element("interfaces");

                    List listOfChild = currentNode.getChildren();
                    Iterator ilistOfChild = listOfChild.iterator();
                    while (ilistOfChild.hasNext()) {
                        Element currentChild = (Element) ilistOfChild.next();

                        if (currentChild.toString().equals("[Element: <description/>]")) {
                            description = Utils.getNameNode("name", currentChild.getText());
                        }

                        if (currentChild.toString().equals("[Element: <status/>]")) {
                            status = Utils.getNameNode("status", currentChild.getText());
                        }

                        if (currentChild.toString().equals("[Element: <rid/>]")) {
                            rid = Utils.getNameNode("rid", currentChild.getText());
                        }

                        if (currentChild.toString().equals("[Element: <type/>]")) {
                            nodeType = Utils.getNameNode("nodeType", "");
                            Attribute type2 = new Attribute("type2", currentChild.getText());
                            nodeType.setAttribute(type2);
                        }

                        if (currentChild.toString().equals("[Element: <location/>]")) {
                            location = Utils.getNameNode("location", "");
                            Attribute latitude = new Attribute("latitude", currentChild.getAttributeValue("latitude"));
                            Attribute longitude = new Attribute("longitude", currentChild.getAttributeValue("longitude"));
                            location.setAttribute(latitude);
                            location.setAttribute(longitude);
                        }

                        if (currentChild.toString().equals("[Element: <interfaces/>]")) {
                            interfaces = Utils.getNameNode("interfaces", "");
                                    

                            List listOfInterface = currentChild.getChildren();
                            Iterator ilistOfInterface = listOfInterface.iterator();
                            while (ilistOfInterface.hasNext()) {
                                Element currentInterface = (Element) ilistOfInterface.next();
                                Element interfaceNode = new Element("interface");

                                List listOfChildOfInterface = currentInterface.getChildren();
                                Iterator ilistOfChildOfInterface = listOfChildOfInterface.iterator();
                                while (ilistOfChildOfInterface.hasNext()) {
                                    Element currentChildOfInterface = (Element) ilistOfChildOfInterface.next();

                                    if (currentChildOfInterface.toString().equals("[Element: <status/>]")) {
                                        Element statusInterface = new Element("status");
                                        statusInterface.setText(currentChildOfInterface.getText());
                                        interfaceNode.addContent(statusInterface);
                                    }

                                    if (currentChildOfInterface.toString().equals("[Element: <ip/>]")) {
                                        Element ip = new Element("ip");
                                        Attribute mask = new Attribute("mask", currentChildOfInterface.getAttributeValue("mask"));
                                        ip.setText(currentChildOfInterface.getText());
                                        ip.setAttribute(mask);
                                        interfaceNode.addContent(ip);
                                    }
                                }
                                Attribute idInterface = new Attribute("id", currentInterface.getAttributeValue("id"));
                                interfaceNode.setAttribute(idInterface);
                                interfaces.addContent(interfaceNode);
                            }
                        }
                    }
                    Attribute idNode = new Attribute("id", currentNode.getAttributeValue("id"));
                    node.setAttribute(idNode);

                    Element switchNode = Utils.getNameNode("switch", "false");
                             
                    if (!description.getText().equals("")) {
                        node.addContent(description);
                    }else{
                        node.addContent(Utils.getNameNode("name", currentNode.getAttributeValue("id")));
                    }
                    if (!status.getText().equals("")) {
                        node.addContent(status);
                    }
                    if (!rid.getText().equals("")) {
                        node.addContent(rid);
                    }
                    if (!switchNode.getText().equals("")) {
                        node.addContent(switchNode);
                    }
                    if (interfaces.getChildren().isEmpty() == false) {
                        node.addContent(interfaces);
                    }
                    if (nodeType.getAttributes().isEmpty() == false) {
                        node.addContent(nodeType);
                    }
                    if (location.getAttributes().isEmpty() == false) {
                        node.addContent(location);
                    }

                    nodes.addContent(node);
                }

                /* links */
                List listOfLink = linksTopoTOTEM.getChildren();
                Iterator ilistOfLink = listOfLink.iterator();
                while (ilistOfLink.hasNext()) {
                    Element currentLink = (Element) ilistOfLink.next();
                    Element link = new Element("edge");
                    Element source = new Element("src");
                    Element target = new Element("tgt");
                    Element description = new Element("description");
                    Element status = new Element("status");
                    Element bandwidth = new Element("bandwidth");
                    Element delay = new Element("delay");
                    Element linkType = new Element("linkType");
                    Element technology = new Element("technology");
                    Element srlgs = new Element("srlgs");

                    Attribute src_node = null;
                    Attribute tgt_node = null;

                    List listOfChildOfLink = currentLink.getChildren();
                    Iterator ilistOfChildOfLink = listOfChildOfLink.iterator();
                    while (ilistOfChildOfLink.hasNext()) {
                        Element currentChild = (Element) ilistOfChildOfLink.next();

                        if (currentChild.toString().equals("[Element: <from/>]")) {
                            src_node = new Attribute("source", currentChild.getAttributeValue("node"));
                            link.setAttribute(src_node);
                            if (currentChild.getAttributes().size() > 1) {
                                source = Utils.getNameNode("src", "");

                                Attribute interf = new Attribute("interface", currentChild.getAttributeValue("if"));
                                source.setAttribute(interf);
                            }
                        }

                        if (currentChild.toString().equals("[Element: <to/>]")) {
                            tgt_node = new Attribute("target", currentChild.getAttributeValue("node"));
                            link.setAttribute(tgt_node);
                            //target.setAttribute(node);
                            if (currentChild.getAttributes().size() > 1) {
                                target = Utils.getNameNode("tgt", "");
                                 
                                Attribute interf = new Attribute("interface", currentChild.getAttributeValue("if"));
                                target.setAttribute(interf);
                            }
                        }

                        if (currentChild.toString().equals("[Element: <status/>]")) {
                            status = Utils.getNameNode("status", currentChild.getText());
                        }

                        if (currentChild.toString().equals("[Element: <description/>]")) {
                            description = Utils.getNameNode("description", currentChild.getText());
                        }

                        if (currentChild.toString().equals("[Element: <type/>]")) {
                            linkType = Utils.getNameNode("linkType", "");
                            Attribute type4 = new Attribute("type4", currentChild.getText());
                            linkType.setAttribute(type4);
                        }

                        if (currentChild.toString().equals("[Element: <bw/>]")) {
                            bandwidth = Utils.getNameNode("bandwidth", ((Double.parseDouble(currentChild.getText())) * unitBandwidth) + "");
                        }

                        if (currentChild.toString().equals("[Element: <technology/>]")) {
                            technology = Utils.getNameNode("technology", currentChild.getText());
                                    
                        }

                        if (currentChild.toString().equals("[Element: <delay/>]")) {
                            delay = Utils.getNameNode("delay", ((Double.parseDouble(currentChild.getText())) * unitDelay) + "");
                        }

                        if (currentChild.toString().equals("[Element: <srlgs/>]")) {
                            srlgs = new Element("srlgs");

                            List listOfSrlg = currentChild.getChildren();
                            Iterator ilistOfSrlg = listOfSrlg.iterator();
                            while (ilistOfSrlg.hasNext()) {
                                Element currentSrlg = (Element) ilistOfSrlg.next();
                                Element srlg = new Element("srlg");
                                srlg.setText(currentSrlg.getText());
                                srlgs.addContent(srlg);
                            }
                        }

                    }
                    Attribute idLink = new Attribute("id", currentLink.getAttributeValue("id"));

                    link.setAttribute(idLink);
                    if (source.getAttributes().isEmpty() == false) {
                        link.addContent(source);
                    }
                    if (target.getAttributes().isEmpty() == false) {
                        link.addContent(target);
                    }
                    if (!description.getText().equals("")) {
                        link.addContent(description);
                    }
                    if (!status.getText().equals("")) {
                        link.addContent(status);
                    }
                    if (!bandwidth.getText().equals("")) {
                        link.addContent(bandwidth);
                    }
                    if (!delay.getText().equals("")) {
                        link.addContent(delay);
                    }
                    if (!linkType.getAttributes().isEmpty() == false) {
                        link.addContent(linkType);
                    }
                    if (!technology.getText().equals("")) {
                        link.addContent(technology);
                    }
                    if (srlgs.getChildren().isEmpty() == false) {
                        link.addContent(srlgs);
                    }

                    links.addContent(link);
                }
            }


            /* MPLS */
            if (BoolMPLS == true) {
                mplsTOTEM = domainTOTEM.getChild("mpls");

                List listOfLspFromMpls = mplsTOTEM.getChildren();
                Iterator ilistOfLspFromMpls = listOfLspFromMpls.iterator();
                while (ilistOfLspFromMpls.hasNext()) {
                    Element currentLsp = (Element) ilistOfLspFromMpls.next();

                    Element lsp = new Element("lsp");
                    Element path = new Element("path");
                    Element bw = new Element("bw");
                    Element metric = new Element("metric");
                    Element maxRate = new Element("max_rate");
                    Element diffServ = new Element("diff-serv");
                    Element acceptedCos = new Element("accepted_cos");
                    Element backup = new Element("backup");

                    List listOfChildOfLsp = currentLsp.getChildren();
                    Iterator ilistOfChildOfLsp = listOfChildOfLsp.iterator();
                    while (ilistOfChildOfLsp.hasNext()) {
                        Element currentChildOfLsp = (Element) ilistOfChildOfLsp.next();

                        if (currentChildOfLsp.toString().equals("[Element: <path/>]")) {
                            path = new Element("path");

                            List listOfChildOfPath = currentChildOfLsp.getChildren();
                            Iterator ilistOfChildOfPath = listOfChildOfPath.iterator();
                            while (ilistOfChildOfPath.hasNext()) {
                                Element currentLinkOfPath = (Element) ilistOfChildOfPath.next();

                                Element link = new Element("link");
                                link.setText(currentLinkOfPath.getText());
                                path.addContent(link);
                            }
                            lsp.addContent(path);
                        }

                        if (currentChildOfLsp.toString().equals("[Element: <bw/>]")) {
                            bw = new Element("bw");
                            bw.setText(((Double.parseDouble(currentChildOfLsp.getText()) * unitBandwidth)) + "");
                            lsp.addContent(bw);
                        }

                        if (currentChildOfLsp.toString().equals("[Element: <metric/>]")) {
                            metric = new Element("metric");
                            metric.setText(currentChildOfLsp.getText());
                            lsp.addContent(metric);
                        }

                        if (currentChildOfLsp.toString().equals("[Element: <max_rate/>]")) {
                            maxRate = new Element("max_rate");
                            maxRate.setText(currentChildOfLsp.getText());
                            lsp.addContent(maxRate);
                        }

                        if (currentChildOfLsp.toString().equals("[Element: <diff-serv/>]")) {
                            diffServ = new Element("diff-serv");

                            List listOfChildOfDiffServ = currentChildOfLsp.getChildren();
                            Iterator ilistOfChildOfDiffServ = listOfChildOfDiffServ.iterator();

                            while (ilistOfChildOfDiffServ.hasNext()) {
                                Element currentChildOfDiffServ = (Element) ilistOfChildOfDiffServ.next();

                                if (currentChildOfDiffServ.toString().equals("[Element: <ct/>]")) {
                                    Element ct = new Element("ct");
                                    ct.setText(currentChildOfDiffServ.getText());
                                    diffServ.addContent(ct);
                                }

                                if (currentChildOfDiffServ.toString().equals("[Element: <preemption/>]")) {
                                    Element preemption = new Element("preemption");
                                    Attribute setup = new Attribute("setup", currentChildOfDiffServ.getAttributeValue("setup"));
                                    Attribute holding = new Attribute("holding", currentChildOfDiffServ.getAttributeValue("holding"));
                                    preemption.setAttribute(setup);
                                    preemption.setAttribute(holding);
                                    diffServ.addContent(preemption);
                                }
                            }
                            lsp.addContent(diffServ);
                        }

                        if (currentChildOfLsp.toString().equals("[Element: <accepted_cos/>]")) {
                            acceptedCos = new Element("accepted_cos");

                            List listOfChildOfAcceptedCos = currentChildOfLsp.getChildren();
                            Iterator ilistOfChildOfAcceptedCos = listOfChildOfAcceptedCos.iterator();

                            while (ilistOfChildOfAcceptedCos.hasNext()) {
                                Element currentChildOfAcceptedCos = (Element) ilistOfChildOfAcceptedCos.next();

                                if (currentChildOfAcceptedCos.toString().equals("[Element: <cos/>]")) {
                                    Element cos = new Element("cos");
                                    cos.setText(currentChildOfAcceptedCos.getText());
                                    acceptedCos.addContent(cos);
                                }
                            }

                            lsp.addContent(acceptedCos);
                        }

                        if (currentChildOfLsp.toString().equals("[Element: <backup/>]")) {
                            backup = new Element("backup");

                            List listOfChildOfBackup = currentChildOfLsp.getChildren();
                            Iterator ilistOfChildOfBackup = listOfChildOfBackup.iterator();
                            while (ilistOfChildOfBackup.hasNext()) {
                                Element currentChildOfBackup = (Element) ilistOfChildOfBackup.next();

                                if (currentChildOfBackup.toString().equals("[Element: <protected_lsp/>]")) {
                                    Element protectedLsp = new Element("protected_lsp");
                                    protectedLsp.setText(currentChildOfBackup.getText());
                                    backup.addContent(protectedLsp);
                                }

                                if (currentChildOfBackup.toString().equals("[Element: <protected_links/>]")) {
                                    Element protectedLinks = new Element("protected_links");

                                    List listOfChildOfProtectedLinks = currentChildOfBackup.getChildren();
                                    Iterator ilistOfChildOfProtectedLinks = listOfChildOfProtectedLinks.iterator();
                                    while (ilistOfChildOfProtectedLinks.hasNext()) {
                                        Element currentChildOfProtectedLinks = (Element) ilistOfChildOfProtectedLinks.next();

                                        if (currentChildOfProtectedLinks.toString().equals("[Element: <protected_link/>]")) {
                                            Element protectedLink = new Element("protected_link");
                                            protectedLink.setText(currentChildOfProtectedLinks.getText());
                                            protectedLinks.addContent(protectedLink);
                                        }
                                    }
                                    backup.addContent(protectedLinks);
                                }
                            }

                            Attribute type = new Attribute("type", currentChildOfLsp.getAttributeValue("type"));
                            backup.setAttribute(type);
                            lsp.addContent(backup);
                        }
                    }
                    mpls.addContent(lsp);
                }
            }


            /* IGP */
            if (BoolIGP == true) {
                igpTOTEM = domainTOTEM.getChild("igp");
                igpLinksTOTEM = igpTOTEM.getChild("links");

                List listOfLinkIgp = igpLinksTOTEM.getChildren();
                Iterator ilistOfLinkIgp = listOfLinkIgp.iterator();
                while (ilistOfLinkIgp.hasNext()) {
                    Element currentLinkIgp = (Element) ilistOfLinkIgp.next();
                    Element igpLink = new Element("link");
                    Element staticLink = new Element("static");
                    Element dynamicLink = new Element("dynamic");
                    Element metric = new Element("metric");
                    Element teMetric = new Element("te-metric");
                    Element mrbw = new Element("mrbw");
                    Element mbw = new Element("mbw");
                    Element admingroup = new Element("admingroup");
                    Element diffServIgp = new Element("diff-serv");
                    Element rbw = new Element("rbw");

                    List listOfChildOfLinkIgp = currentLinkIgp.getChildren();
                    Iterator ilistOfChildOfLinkIgp = listOfChildOfLinkIgp.iterator();
                    while (ilistOfChildOfLinkIgp.hasNext()) {
                        Element currentChildOfLinkIgp = (Element) ilistOfChildOfLinkIgp.next();

                        if (currentChildOfLinkIgp.toString().equals("[Element: <static/>]")) {
                            staticLink = new Element("static");

                            List listOfChildOfStaticInIgpLink = currentChildOfLinkIgp.getChildren();
                            Iterator ilistOfChildOfStaticInIgpLink = listOfChildOfStaticInIgpLink.iterator();
                            while (ilistOfChildOfStaticInIgpLink.hasNext()) {
                                Element currentChildOfStatic = (Element) ilistOfChildOfStaticInIgpLink.next();

                                if (currentChildOfStatic.toString().equals("[Element: <metric/>]")) {
                                    metric = new Element("metric");
                                    metric.setText(currentChildOfStatic.getText());
                                }

                                if (currentChildOfStatic.toString().equals("[Element: <te-metric/>]")) {
                                    teMetric = new Element("te-metric");
                                    teMetric.setText(currentChildOfStatic.getText());
                                }

                                if (currentChildOfStatic.toString().equals("[Element: <mrbw/>]")) {
                                    mrbw = new Element("mrbw");
                                    mrbw.setText(currentChildOfStatic.getText());
                                }

                                if (currentChildOfStatic.toString().equals("[Element: <mbw/>]")) {
                                    mbw = new Element("mbw");
                                    mbw.setText(((Double.parseDouble(currentChildOfStatic.getText()) * unitBandwidth)) + "");
                                }

                                if (currentChildOfStatic.toString().equals("[Element: <admingroup/>]")) {
                                    admingroup = new Element("admingroup");
                                    admingroup.setText(currentChildOfStatic.getText());
                                }

                                if (currentChildOfStatic.toString().equals("[Element: <diff-serv/>]")) {
                                    diffServIgp = new Element("diff-serv");

                                    List listOfChildOfDiffServOfIgpLink = currentChildOfStatic.getChildren();
                                    Iterator ilistOfChildOfDiffServOfIgpLink = listOfChildOfDiffServOfIgpLink.iterator();
                                    while (ilistOfChildOfDiffServOfIgpLink.hasNext()) {
                                        Element currentChildOfDiffServ = (Element) ilistOfChildOfDiffServOfIgpLink.next();

                                        if (currentChildOfDiffServ.toString().equals("[Element: <bcm/>]")) {
                                            Element bcm = new Element("bcm");
                                            bcm.setText(currentChildOfDiffServ.getText());
                                            diffServIgp.addContent(bcm);
                                        }

                                        if (currentChildOfDiffServ.toString().equals("[Element: <bc/>]")) {
                                            Element bc = new Element("bc");
                                            bc.setText(currentChildOfDiffServ.getText());
                                            Attribute id = new Attribute("id", currentChildOfDiffServ.getAttributeValue("id"));
                                            bc.setAttribute(id);
                                            diffServIgp.addContent(bc);
                                        }
                                    }

                                    if (!metric.getText().equals("")) {
                                        staticLink.addContent(metric);
                                    }
                                    if (!teMetric.getText().equals("")) {
                                        staticLink.addContent(teMetric);
                                    }
                                    if (!mrbw.getText().equals("")) {
                                        staticLink.addContent(mrbw);
                                    }
                                    if (!mbw.getText().equals("")) {
                                        staticLink.addContent(mbw);
                                    }
                                    if (!admingroup.getText().equals("")) {
                                        staticLink.addContent(admingroup);
                                    }
                                    if (diffServIgp.getChildren().isEmpty() == false) {
                                        staticLink.addContent(diffServIgp);
                                    }
                                }


                            }
                        }

                        if (currentChildOfLinkIgp.toString().equals("[Element: <dynamic/>]")) {
                            dynamicLink = new Element("dynamic");

                            List listOfChildOfDynamicLink = currentChildOfLinkIgp.getChildren();
                            Iterator ilistOfChildOfDynamicLink = listOfChildOfDynamicLink.iterator();
                            while (ilistOfChildOfDynamicLink.hasNext()) {
                                Element currentChildOfDynamic = (Element) ilistOfChildOfDynamicLink.next();

                                if (currentChildOfDynamic.toString().equals("[Element: <rbw/>]")) {
                                    rbw = new Element("rbw");

                                    List listOfPriority = currentChildOfDynamic.getChildren();
                                    Iterator ilistOfPriority = listOfPriority.iterator();
                                    while (ilistOfPriority.hasNext()) {
                                        Element currentPriority = (Element) ilistOfPriority.next();

                                        Element priority = new Element("priority");
                                        priority.setText(currentPriority.getText());
                                        Attribute id = new Attribute("id", currentPriority.getAttributeValue("id"));
                                        priority.setAttribute(id);
                                        rbw.addContent(priority);
                                    }
                                }

                                if (rbw.getChildren().isEmpty() == false) {
                                    dynamicLink.addContent(rbw);
                                }
                            }

                        }
                    }
                    Attribute idIgpLink = new Attribute("id", currentLinkIgp.getAttributeValue("id"));
                    igpLink.setAttribute(idIgpLink);

                    if (staticLink.getChildren().isEmpty() == false) {
                        igpLink.addContent(staticLink);
                    }
                    if (dynamicLink.getChildren().isEmpty() == false) {
                        igpLink.addContent(dynamicLink);
                    }

                    igpLinks.addContent(igpLink);
                }
            }

            /* BGP */
            if (BoolBGP == true) {
                bgpTOTEM = domainTOTEM.getChild("bgp");
                bgpRoutersTOTEM = bgpTOTEM.getChild("routers");

                List listOfRouter = bgpRoutersTOTEM.getChildren();
                Iterator ilistOfRouter = listOfRouter.iterator();
                while (ilistOfRouter.hasNext()) {
                    Element currentRouter = (Element) ilistOfRouter.next();
                    Element router = new Element("router");
                    Element rid = new Element("rid");
                    Element networks = new Element("networks");
                    Element neighbors = new Element("neighbors");

                    List listOfChildOfRouter = currentRouter.getChildren();
                    Iterator ilistOfChildOfRouter = listOfChildOfRouter.iterator();
                    while (ilistOfChildOfRouter.hasNext()) {
                        Element currentChildOfRouter = (Element) ilistOfChildOfRouter.next();

                        if (currentChildOfRouter.toString().equals("[Element: <rid/>]")) {
                            rid = new Element("rid");
                            rid.setText(currentChildOfRouter.getText());
                            router.addContent(rid);
                        }

                        if (currentChildOfRouter.toString().equals("[Element: <networks/>]")) {
                            networks = new Element("networks");

                            List listOfNetwork = currentChildOfRouter.getChildren();
                            Iterator ilistOfNetwork = listOfNetwork.iterator();
                            while (ilistOfNetwork.hasNext()) {
                                Element currentNetwork = (Element) ilistOfNetwork.next();

                                if (currentNetwork.toString().equals("[Element: <network/>]")) {
                                    Element network = new Element("network");
                                    Attribute prefix = new Attribute("prefix", currentNetwork.getAttributeValue("prefix"));
                                    network.setAttribute(prefix);
                                    networks.addContent(network);
                                }
                            }
                            router.addContent(networks);
                        }

                        if (currentChildOfRouter.toString().equals("[Element: <neighbors/>]")) {
                            neighbors = new Element("neighbors");

                            List listOfNeighbors = currentChildOfRouter.getChildren();
                            Iterator ilistOfNeighbors = listOfNeighbors.iterator();
                            while (ilistOfNeighbors.hasNext()) {
                                Element currentNeighbor = (Element) ilistOfNeighbors.next();

                                Element neighbor = new Element("neighbor");

                                List listOfFilters = currentNeighbor.getChildren();
                                Iterator ilistOfFilters = listOfFilters.iterator();
                                while (ilistOfFilters.hasNext()) {
                                    Element currentFilters = (Element) ilistOfFilters.next();

                                    if (currentFilters.toString().equals("[Element: <filters/>]")) {
                                        Element filters = new Element("filters");

                                        List listOfFilter = currentFilters.getChildren();
                                        Iterator ilistOfFilter = listOfFilter.iterator();
                                        while (ilistOfFilter.hasNext()) {
                                            Element currentFilter = (Element) ilistOfFilter.next();

                                            if (currentFilter.toString().equals("[Element: <in-filter/>]")) {
                                                Element inFilter = new Element("in-filter");
                                                Element rule = new Element("rule");

                                                List listOfActionsOfRule = currentFilter.getChild("rule").getChildren();
                                                Iterator ilistOfActionsOfRule = listOfActionsOfRule.iterator();

                                                while (ilistOfActionsOfRule.hasNext()) {
                                                    Element currentAction = (Element) ilistOfActionsOfRule.next();

                                                    if (currentAction.toString().equals("[Element: <action/>]")) {
                                                        Element action = new Element("action");
                                                        Attribute type = new Attribute("type", currentAction.getAttributeValue("type"));
                                                        Attribute value = new Attribute("value", currentAction.getAttributeValue("value"));
                                                        action.setAttribute(type);
                                                        action.setAttribute(value);
                                                        rule.addContent(action);
                                                    }
                                                }
                                                inFilter.addContent(rule);
                                                filters.addContent(inFilter);
                                            }

                                            if (currentFilter.toString().equals("[Element: <out-filter/>]")) {
                                                Element outFilter = new Element("out-filter");
                                                Element rule = new Element("rule");

                                                List listOfActionsOfRule = currentFilter.getChild("rule").getChildren();
                                                Iterator ilistOfActionsOfRule = listOfActionsOfRule.iterator();

                                                while (ilistOfActionsOfRule.hasNext()) {
                                                    Element currentAction = (Element) ilistOfActionsOfRule.next();

                                                    if (currentAction.toString().equals("[Element: <action/>]")) {
                                                        Element action = new Element("action");
                                                        Attribute type = new Attribute("type", currentAction.getAttributeValue("type"));
                                                        Attribute value = new Attribute("value", currentAction.getAttributeValue("value"));
                                                        action.setAttribute(type);
                                                        action.setAttribute(value);
                                                        rule.addContent(action);
                                                    }
                                                }
                                                outFilter.addContent(rule);
                                                filters.addContent(outFilter);
                                            }
                                        }
                                    }
                                }

                                Attribute AS = new Attribute("as", currentNeighbor.getAttributeValue("as"));
                                neighbor.setAttribute(AS);
                                Attribute ip = new Attribute("ip", currentNeighbor.getAttributeValue("ip"));
                                neighbor.setAttribute(ip);

                                List listOfAttributeOfNeighbor = currentNeighbor.getAttributes();
                                Iterator ilistOfAttributeOfNeighbor = listOfAttributeOfNeighbor.iterator();
                                while (ilistOfAttributeOfNeighbor.hasNext()) {
                                    Attribute currentAttribute = (Attribute) ilistOfAttributeOfNeighbor.next();

                                    if (currentAttribute.getName().equals("reflector-client")) {
                                        Attribute reflectorClient = new Attribute("reflector-client", currentNeighbor.getAttributeValue("reflector-client"));
                                        neighbor.setAttribute(reflectorClient);
                                    }

                                    if (currentAttribute.getName().equals("next-hop-self")) {
                                        Attribute nextHopSelf = new Attribute("next-hop-self", currentNeighbor.getAttributeValue("next-hop-self"));
                                        neighbor.setAttribute(nextHopSelf);
                                    }
                                }
                                neighbors.addContent(neighbor);
                            }
                            router.addContent(neighbors);
                        }
                    }
                    List listOfAttributesOfRouter = currentRouter.getAttributes();
                    Iterator ilistOfAttributesOfRouter = listOfAttributesOfRouter.iterator();
                    while (ilistOfAttributesOfRouter.hasNext()) {
                        Attribute currentAttributeOfRouter = (Attribute) ilistOfAttributesOfRouter.next();

                        if (currentAttributeOfRouter.getName().equals("reflector")) {
                            Attribute reflector = new Attribute("reflector", currentRouter.getAttributeValue("reflector"));
                            router.setAttribute(reflector);
                        }

                        if (currentAttributeOfRouter.getName().equals("id")) {
                            Attribute id = new Attribute("id", currentRouter.getAttributeValue("id"));
                            router.setAttribute(id);
                        }
                    }
                    bgpRouters.addContent(router);
                }
            }

            if (BoolInformation == true) {
                domain.addContent(information);
            }

            if (BoolTopology == true) {
                topology.addContent(nodes);
                topology.addContent(links);
                
                domain.addContent(topology);
            }

            if (BoolMPLS == true) {
                domain.addContent(mpls);
            }

            if (BoolIGP == true) {
                igp.addContent(igpLinks);
                domain.addContent(igp);
            }

            if (BoolBGP == true) {
                bgp.addContent(bgpRouters);
                domain.addContent(bgp);
            }

            List attributesDomain = domainTOTEM.getAttributes();
            Iterator iattributesDomain = attributesDomain.iterator();
            while (iattributesDomain.hasNext()) {
                Attribute currentAttribute = (Attribute) iattributesDomain.next();
                //System.out.println("Attribut domain : "+currentAttribute.getName());
                if (currentAttribute.getName().equals("ASID")) {
                    Attribute ASID = new Attribute("ASID", currentAttribute.getValue());
                    domain.setAttribute(ASID);
                }
                if (currentAttribute.getName().equals("name")) {
                    Attribute name = new Attribute("name", currentAttribute.getValue());
                    domain.setAttribute(name);
                }
                if (currentAttribute.getName().equals("xmlns:xsi")) {
                    Attribute xmlns = new Attribute("xmlns:xsi", currentAttribute.getValue());
                    domain.setAttribute(xmlns);
                }
                if (currentAttribute.getName().equals("xsi:noNamespaceSchemaLocation")) {
                    Attribute xsi = new Attribute("xsi:noNamespaceSchemaLocation", currentAttribute.getValue());
                    domain.setAttribute(xsi);
                }
            }


            


            graph.addContent(domain);
            graphml.addContent(graph);

            org.jdom.output.XMLOutputter sortie = new org.jdom.output.XMLOutputter(org.jdom.output.Format.getPrettyFormat());
            sortie.output(document, new FileOutputStream(fileOut.getAbsolutePath()));

            result.add("ParserTOTEM", "Import succeeded");

        } catch (Exception e) {
            e.printStackTrace();
            result.add("ParserTOTEM", "Import had problems " + e);
        }


    }
}
